home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp 2.0.1 (Many Libraries) / Libraries / UMacApp.TEvtHandler.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  15.7 KB  |  599 lines  |  [TEXT/MPS ]

  1. {$P}
  2. {[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n-]}
  3. { UMacApp.TEvtHandler.p }
  4. { Copyright © 1984-1990 by Apple Computer Inc.    All rights reserved. }
  5.  
  6. {--------------------------------------------------------------------------------------------------}
  7. {$S MAEvtHandlerRes}
  8.  
  9. PROCEDURE TEvtHandler.IEvtHandler(itsNextHandler: TEvtHandler);
  10.  
  11.     BEGIN
  12.     IObject;
  13.  
  14.     fNextHandler := itsNextHandler;
  15.     fIdleFreq := kMaxIdleTime;                            { Assume it never wants idle time }
  16.     fLastIdle := 0;
  17.     END;
  18.  
  19. {--------------------------------------------------------------------------------------------------}
  20. {$S MAClose}
  21.  
  22. PROCEDURE TEvtHandler.Free; OVERRIDE;
  23.  
  24.     BEGIN
  25.     IF gTarget = SELF THEN
  26.         IF fNextHandler = NIL THEN
  27.             gApplication.SetTarget(gApplication)
  28.         ELSE
  29.             gApplication.SetTarget(fNextHandler);
  30.  
  31.     fNextHandler := NIL;
  32.     INHERITED Free;
  33.     END;
  34.  
  35. {--------------------------------------------------------------------------------------------------}
  36. {$S MANonRes}
  37.  
  38. FUNCTION TEvtHandler.AddHandler(headOfChain: TEvtHandler): TEvtHandler;
  39.  
  40.     BEGIN
  41.     fNextHandler := headOfChain;
  42.     AddHandler := SELF;                                 { new head of chain }
  43.     END;
  44.  
  45. {--------------------------------------------------------------------------------------------------}
  46. {$S MANonRes}
  47.  
  48. FUNCTION TEvtHandler.RemoveHandler(headOfChain: TEvtHandler): TEvtHandler;
  49.  
  50.     VAR
  51.         prevCohandler:        TEvtHandler;
  52.         currCohandler:        TEvtHandler;
  53.  
  54.     BEGIN
  55.     prevCohandler := NIL;
  56.     currCohandler := headOfChain;
  57.     RemoveHandler := headOfChain;
  58.     WHILE currCohandler <> NIL DO
  59.         BEGIN
  60.         IF currCohandler = SELF THEN                    { found it }
  61.             BEGIN
  62.             IF prevCohandler = NIL THEN                 { I was the head of the chain, so there will
  63.                                                          be a new head of chain }
  64.                 RemoveHandler := fNextHandler
  65.             ELSE
  66.                 prevCohandler.fNextHandler := fNextHandler; { take me out of the link }
  67.             fNextHandler := NIL;                        { remember that i'm not in the chain anymore
  68.                                                          }
  69.             currCohandler := NIL;                        { So loop will end }
  70.             END
  71.         ELSE
  72.             BEGIN
  73.             prevCohandler := currCohandler;
  74.             currCohandler := currCohandler.fNextHandler;
  75.             END;
  76.         END;
  77.     END;
  78.  
  79. {--------------------------------------------------------------------------------------------------}
  80. {$S MAFields}
  81.  
  82. PROCEDURE TEvtHandler.Fields(PROCEDURE DoToField(fieldName: Str255;
  83.                                                  fieldAddr: Ptr;
  84.                                                  fieldType: INTEGER));
  85.  
  86.     BEGIN
  87.     DoToField('TEvtHandler', NIL, bClass);
  88.     DoToField('fNextHandler', @fNextHandler, bObject);
  89.     DoToField('fIdleFreq', @fIdleFreq, bLongInt);
  90.     DoToField('fLastIdle', @fLastIdle, bLongInt);
  91.     INHERITED Fields(DoToField);
  92.     END;
  93.  
  94. {--------------------------------------------------------------------------------------------------}
  95. {$S MAOpen}
  96.  
  97. FUNCTION TEvtHandler.CreateAView(itsDocument: TDocument;
  98.                                  itsSuperView: TView;
  99.                                  VAR itsParams: Ptr): TView;
  100. {!!! Seems like there should be a class that is common to views, documents and application
  101. like TEvtHandler, but without cluttering TEvtHandler with lots of detail crud like this }
  102.  
  103.     VAR
  104.         aView:                TView;
  105.  
  106.     BEGIN
  107.     IF fNextHandler <> NIL THEN
  108.         aView := fNextHandler.CreateAView(itsDocument, itsSuperView, itsParams)
  109.     ELSE
  110.         BEGIN
  111.         WITH ViewTemplatePtr(itsParams)^ DO
  112.             IF itsType <> '' THEN
  113.                 BEGIN
  114.                 aView := TView(NewObjectByClassName(itsType));
  115.                 IF (aView = NIL) & (GetClassIDFromName(itsType) = kNilClass) THEN
  116.                     BEGIN
  117.                     {$IFC qDebug}
  118.                     ProgramBreak(CONCAT('The application doesn’t contain the class ‘',
  119.                                         ViewTemplatePtr(itsParams)^.itsType, '.’'));
  120.                     {$ENDC}
  121.                     gErrorParm3 := itsType;    { show name of class }
  122.                     Failure(errMissingClass, 0);
  123.                     END;
  124.                 END
  125.             ELSE
  126.                 aView := TView(NewStdObject(itsSignature));
  127.  
  128.         IF aView <> NIL THEN
  129.             aView.IRes(itsDocument, itsSuperView, itsParams);
  130.         END;
  131.  
  132.     FailNIL(aView);
  133.     CreateAView := aView;
  134.     END;
  135.  
  136. {--------------------------------------------------------------------------------------------------}
  137. {$S MAEvtHandlerRes}
  138.  
  139. PROCEDURE TEvtHandler.DoChoice(origView: TView;
  140.                                itsChoice: INTEGER);
  141.  
  142.     BEGIN
  143.     IF fNextHandler <> NIL THEN
  144.         fNextHandler.DoChoice(origView, itsChoice)
  145.     ELSE IF gIntenseDebugging THEN
  146.         BEGIN
  147.         Write('in TEvtHandler.DoChoice: no one handled the choice: ', itsChoice);
  148.         WrLblHexLongint(' From view: ', ord(origView));
  149.         WriteLn;
  150.         END;
  151.     END;
  152.  
  153. {--------------------------------------------------------------------------------------------------}
  154. {$S MAOpen}
  155.  
  156. FUNCTION TEvtHandler.DoCreateViews(itsDocument: TDocument;
  157.                                    parentView: TView;
  158.                                    itsRsrcID: INTEGER;
  159.                                    subviewOffset: VPoint): TView;
  160.  
  161.     VAR
  162.         i:                    INTEGER;
  163.         numViews:            INTEGER;
  164.         aView:                TView;
  165.         viewResource:        ViewRsrcHndl;
  166.         theViewInfo:        ViewTemplatePtr;
  167.         lastParentID:        IDType;
  168.         lastParent:         TView;
  169.         lastRoot:            TView;
  170.         firstView:            TView;
  171.         fi:                 FailInfo;
  172.  
  173.     PROCEDURE HdlDoCreateViews(error: OSErr;
  174.                                message: LONGINT);
  175.  
  176.         BEGIN
  177.         IF viewResource <> NIL THEN                     { Don't constipate the heap }
  178.             HUnLock(Handle(viewResource));
  179.  
  180.         FreeIfObject(firstView);
  181.         firstView := NIL;
  182.         END;
  183.  
  184.     {$IFC qDebug}
  185.  
  186.     PROCEDURE ReportTemplate;
  187.  
  188.         BEGIN
  189.         WITH theViewInfo^ DO
  190.             BEGIN
  191.             WrLblSig('signature', itsSignature);
  192.             WriteLn;
  193.             WrLblSig('itsParentID', itsParentID);
  194.             WrLblSig(', thisViewID', thisViewID);
  195.             WriteLn;
  196.             WrLblVPt('itsLocation', itsLocation);
  197.             WrLblVPt(', itsSize', itsSize);
  198.             Write('itsHSizeDet = ', ord(itsHSizeDet): 3);
  199.             WriteLn(', itsVSizeDet = ', ord(itsVSizeDet): 3);
  200.             WrLblBoolean(', isEnabled ', isEnabled);
  201.             WriteLn;
  202.             WriteLn('----------  end of view  ----------');
  203.             END;
  204.         END;
  205.     {$ENDC}
  206.  
  207.     BEGIN
  208.     IF fNextHandler <> NIL THEN
  209.         DoCreateViews := fNextHandler.DoCreateViews(itsDocument, parentView, itsRsrcID,
  210.                                                     subviewOffset)
  211.     ELSE
  212.         BEGIN
  213.         firstView := NIL;                                { Assume the worst. }
  214.  
  215.         viewResource := ViewRsrcHndl(GetResource('view', itsRsrcID));
  216.         IF viewResource = NIL THEN
  217.             BEGIN
  218.             {$IFC qDebug}
  219.             ProgramBreak(ConcatNumber('Unable to find ‘view’ resource #', itsRsrcID));
  220.             {$ENDC}
  221.             FailNilResource(viewResource);
  222.             END;
  223.  
  224.         LockHandleHigh(Handle(viewResource));
  225.  
  226.         CatchFailures(fi, HdlDoCreateViews);
  227.  
  228.         numViews := viewResource^^.numViews;
  229.         theViewInfo := @viewResource^^.theViews;
  230.         lastParentID := kNoIdentifier;
  231.         aView := parentView;
  232.         lastRoot := parentView;
  233.  
  234.         FOR i := 1 TO numViews DO
  235.             WITH theViewInfo^ DO
  236.                 BEGIN
  237.                 {$IFC qDebug}
  238.                 IF gIntenseDebugging THEN
  239.                     ReportTemplate;
  240.                 {$ENDC}
  241.  
  242.                 IF LONGINT(itsParentID) = LONGINT(kNoIdentifier) THEN
  243.                     lastParent := parentView
  244.                 ELSE IF LONGINT(itsParentID) <> LONGINT(lastParentID) THEN
  245.                     BEGIN
  246.                     lastParent := aView;                { Begin with last view created or parentView
  247.                                                          }
  248.                     WHILE (lastParent <> NIL) & (lastParent.fIdentifier <> itsParentID) DO
  249.                         lastParent := lastParent.fSuperView;
  250.  
  251.                     IF (lastParent = NIL) & (lastRoot <> NIL) THEN
  252.                         IF aView <> NIL THEN
  253.                             lastParent := aView.FindSubView(itsParentID)
  254.                         ELSE
  255.                             lastParent := lastRoot.FindSubView(itsParentID);
  256.  
  257.                     {$IFC qDebug}
  258.                     IF lastParent = NIL THEN
  259.                         ProgramBreak('Unable to find parent view for template');
  260.                     {$ENDC}
  261.                     END;
  262.                 lastParentID := itsParentID;
  263.  
  264.                 IF LONGINT(itsSignature) = LONGINT('incl') THEN
  265.                     BEGIN
  266.                     aView := DoCreateViews(itsDocument, lastParent, includeRsrcID, gZeroVPt);
  267.                     OffsetPtr(theViewInfo, SIZEOF(ViewTemplate) - SIZEOF(Str255) + SIZEOF(INTEGER));
  268.                     END
  269.                 ELSE IF LONGINT(itsSignature) = LONGINT('inc@') THEN
  270.                     BEGIN
  271.                     aView := DoCreateViews(itsDocument, lastParent, includeRsrcID,
  272.                              itsSubViewOffset);
  273.                     OffsetPtr(theViewInfo, SIZEOF(ViewTemplate) - SIZEOF(Str255) +
  274.                               SIZEOF(INTEGER) + SIZEOF(VPoint));
  275.                     END
  276.                 ELSE
  277.                     aView := CreateAView(itsDocument, lastParent, Ptr(theViewInfo));
  278.  
  279.                 IF aView = NIL THEN
  280.                     LEAVE;
  281.                 IF ((subviewOffset.h <> 0) | (subviewOffset.v <> 0)) & (aView.fSuperView =
  282.                    parentView) & (parentView <> NIL) THEN
  283.                     aView.Locate(aView.fLocation.h + subviewOffset.h, aView.fLocation.v +
  284.                                  subviewOffset.v, kDontInvalidate);
  285.  
  286.                 IF i = 1 THEN
  287.                     BEGIN
  288.                     firstView := aView;
  289.                     IF Member(aView, TWindow) & (parentView = NIL) THEN
  290.                         parentView := aView;
  291.                     END;
  292.  
  293.                 IF (lastRoot = NIL) & (aView <> NIL) & (aView.fSuperView = NIL) THEN
  294.                     lastRoot := aView;
  295.                 END;
  296.  
  297.         HUnLock(Handle(viewResource));
  298.         Success(fi);
  299.         IF firstView <> NIL THEN
  300.             firstView.AdjustSize;                        { Make sure size gets adjusted by the size
  301.                                                          determiners }
  302.         DoCreateViews := firstView;
  303.         END;
  304.     END;
  305.  
  306. {--------------------------------------------------------------------------------------------------}
  307. {$S MAEvtHandlerRes}
  308.  
  309. FUNCTION TEvtHandler.DoCommandKey(ch: Char;
  310.                                   VAR info: EventInfo): TCommand;
  311.  
  312.     BEGIN
  313.     IF fNextHandler <> NIL THEN
  314.         DoCommandKey := fNextHandler.DoCommandKey(ch, info)
  315.     ELSE
  316.         DoCommandKey := NIL;
  317.     END;
  318.  
  319. {--------------------------------------------------------------------------------------------------}
  320. {$S MAEvtHandlerRes}
  321.  
  322. FUNCTION TEvtHandler.DoHandleEvent(nextEvent: EventRecordPtr;
  323.                                    VAR commandToPerform: TCommand): BOOLEAN;
  324.  
  325.     BEGIN
  326.     DoHandleEvent := FALSE;
  327.     END;
  328.  
  329. {--------------------------------------------------------------------------------------------------}
  330. {$S MAHelp}
  331.  
  332. FUNCTION TEvtHandler.DoHelp(VAR info: EventInfo;
  333.                             VAR message: UNIV LONGINT): TCommand;
  334.  
  335.     BEGIN
  336.     IF fNextHandler <> NIL THEN
  337.         DoHelp := fNextHandler.DoHelp(info, message)
  338.     ELSE
  339.         DoHelp := NIL;
  340.     END;
  341.  
  342. {--------------------------------------------------------------------------------------------------}
  343. {$S MAEvtHandlerRes}
  344.  
  345. FUNCTION TEvtHandler.DoIdle(phase: IdlePhase): BOOLEAN;
  346.  
  347.     BEGIN
  348.     DoIdle := FALSE;                                    { Did not free myself }
  349.     END;
  350.  
  351. {--------------------------------------------------------------------------------------------------}
  352. {$S MAEvtHandlerRes}
  353.  
  354. FUNCTION TEvtHandler.DoKeyCommand(ch: Char;
  355.                                   aKeyCode: INTEGER;
  356.                                   VAR info: EventInfo): TCommand;
  357.  
  358.     BEGIN
  359.     IF fNextHandler <> NIL THEN
  360.         DoKeyCommand := fNextHandler.DoKeyCommand(ch, aKeyCode, info)
  361.     ELSE
  362.         DoKeyCommand := NIL;                            {??? give an error ???}
  363.     END;
  364.  
  365. {--------------------------------------------------------------------------------------------------}
  366. {$S MASelCommand}
  367.  
  368. FUNCTION TEvtHandler.DoMenuCommand(aCmdNumber: CmdNumber): TCommand;
  369.  
  370.     BEGIN
  371.     IF fNextHandler <> NIL THEN
  372.         DoMenuCommand := fNextHandler.DoMenuCommand(aCmdNumber)
  373.     ELSE
  374.         BEGIN
  375.         {$IFC qDebug}
  376.         WriteLn('No one handled the command ', aCmdNumber: 1);
  377.         {$ENDC}
  378.         DoMenuCommand := NIL;
  379.         END;
  380.     END;
  381.  
  382. {--------------------------------------------------------------------------------------------------}
  383. {$S MAEvtHandlerRes}
  384.  
  385. FUNCTION TEvtHandler.DoMultiClick(lastDownPt, newDownPt: Point): BOOLEAN;
  386.  
  387.     BEGIN
  388.     IF fNextHandler <> NIL THEN
  389.         DoMultiClick := fNextHandler.DoMultiClick(lastDownPt, newDownPt)
  390.     ELSE
  391.         DoMultiClick := (ABS(lastDownPt.h - newDownPt.h) <= (gStdHysteresis.h)) &
  392.                         (ABS(lastDownPt.v - newDownPt.v) <= (gStdHysteresis.v));
  393.     END;
  394.  
  395. {--------------------------------------------------------------------------------------------------}
  396. {$S MAEvtHandlerRes}
  397.  
  398. PROCEDURE TEvtHandler.DoSetupMenus;
  399.  
  400.     BEGIN
  401.     IF fNextHandler <> NIL THEN
  402.         fNextHandler.DoSetupMenus;
  403.     END;
  404.  
  405. {--------------------------------------------------------------------------------------------------}
  406. {$S MAEvtHandlerRes}
  407.  
  408. PROCEDURE TEvtHandler.EachHandler(PROCEDURE DoToEvtHandler(anEvtHandler: TEvtHandler));
  409.  
  410.     VAR
  411.         currHandler:        TEvtHandler;
  412.         nextHandler:        TEvtHandler;
  413.  
  414.     BEGIN
  415.     currHandler := SELF;
  416.     WHILE currHandler <> NIL DO
  417.         BEGIN
  418.         { Get next handler now, in case DoToEvtHandler frees currHandler }
  419.         nextHandler := currHandler.fNextHandler;
  420.         DoToEvtHandler(currHandler);
  421.         currHandler := nextHandler;
  422.         END;
  423.     END;
  424.  
  425. {--------------------------------------------------------------------------------------------------}
  426. {$S MAEvtHandlerRes}
  427.  
  428. FUNCTION TEvtHandler.FirstHandlerThat(FUNCTION TestEvtHandler(anEvtHandler: TEvtHandler): BOOLEAN):
  429.                                       TEvtHandler;
  430.  
  431.     VAR
  432.         currHandler:        TEvtHandler;
  433.         nextHandler:        TEvtHandler;
  434.  
  435.     BEGIN
  436.     currHandler := SELF;
  437.     WHILE currHandler <> NIL DO
  438.         BEGIN
  439.         { Get next handler now, in case DoToEvtHandler free currHandler }
  440.         nextHandler := currHandler.fNextHandler;
  441.         IF TestEvtHandler(currHandler) THEN
  442.             BEGIN
  443.             FirstHandlerThat := currHandler;
  444.             EXIT(FirstHandlerThat);
  445.             END;
  446.         currHandler := nextHandler;
  447.         END;
  448.     FirstHandlerThat := NIL;
  449.     END;
  450.  
  451. {--------------------------------------------------------------------------------------------------}
  452. {$S MAEvtHandlerRes}
  453.  
  454. FUNCTION TEvtHandler.HandlesPrintingCommands: BOOLEAN;
  455.  
  456.     BEGIN
  457.     IF fNextHandler <> NIL THEN
  458.         HandlesPrintingCommands := fNextHandler.HandlesPrintingCommands
  459.     ELSE
  460.         HandlesPrintingCommands := FALSE
  461.     END;
  462.  
  463. {--------------------------------------------------------------------------------------------------}
  464. {$S MADebug}
  465.  
  466. PROCEDURE TEvtHandler.IdentifySoftware;
  467.  
  468.     BEGIN
  469.     IF fNextHandler <> NIL THEN
  470.         fNextHandler.IdentifySoftware;
  471.     END;
  472.  
  473. {--------------------------------------------------------------------------------------------------}
  474. {$S MAActivate}
  475.  
  476. PROCEDURE TEvtHandler.InstallSelection(wasActive, beActive: BOOLEAN);
  477.  
  478.     BEGIN
  479.     END;
  480.  
  481. {--------------------------------------------------------------------------------------------------}
  482. {$S MADebug}
  483.  
  484. FUNCTION TEvtHandler.LookupSymbol(VAR sym: Str255): LONGINT;
  485.  
  486.     BEGIN
  487.     IF fNextHandler <> NIL THEN
  488.         LookupSymbol := fNextHandler.LookupSymbol(sym)
  489.     ELSE
  490.         LookupSymbol := - 1;
  491.     END;
  492.  
  493. {--------------------------------------------------------------------------------------------------}
  494. {$S MAEvtHandlerRes}
  495.  
  496. PROCEDURE TEvtHandler.CommitLastCommand;
  497.  
  498.     BEGIN
  499.     IF fNextHandler <> NIL THEN
  500.         fNextHandler.CommitLastCommand;
  501.     END;
  502.  
  503. {--------------------------------------------------------------------------------------------------}
  504. {$S MAEvtHandlerRes}
  505.  
  506. FUNCTION TEvtHandler.GetNextCommand: TCommand;
  507.  
  508.     BEGIN
  509.     IF fNextHandler <> NIL THEN
  510.         GetNextCommand := fNextHandler.GetNextCommand
  511.     ELSE
  512.         GetNextCommand := NIL;
  513.     END;
  514.  
  515. {--------------------------------------------------------------------------------------------------}
  516. {$S MAEvtHandlerRes}
  517.  
  518. FUNCTION TEvtHandler.GetLastCommand: TCommand;
  519.  
  520.     BEGIN
  521.     IF fNextHandler <> NIL THEN
  522.         GetLastCommand := fNextHandler.GetLastCommand
  523.     ELSE
  524.         GetLastCommand := NIL;
  525.     END;
  526.  
  527. {--------------------------------------------------------------------------------------------------}
  528. {$S MAEvtHandlerRes}
  529.  
  530. PROCEDURE TEvtHandler.PerformCommand(command: TCommand);
  531.  
  532.     BEGIN
  533.     IF fNextHandler <> NIL THEN
  534.         fNextHandler.PerformCommand(command)
  535.     ELSE
  536.         BEGIN
  537.         {$IFC qDebug}
  538.         WrLblHexLongint('No one performed the command ', ord(command));
  539.         {$ENDC}
  540.         IF command.fFreeOnCompletion THEN
  541.             FreeIfObject(command);
  542.         END;
  543.     END;
  544.  
  545. {--------------------------------------------------------------------------------------------------}
  546. {$S MAEvtHandlerRes}
  547.  
  548. PROCEDURE TEvtHandler.SetIdleFreq(newIdleFreq: LONGINT);
  549.  
  550.     BEGIN
  551.     fIdleFreq := newIdleFreq;
  552.     END;
  553.  
  554. {--------------------------------------------------------------------------------------------------}
  555. {$S MAEvtHandlerRes}
  556.  
  557. PROCEDURE TEvtHandler.PostCommand(command: TCommand);
  558.  
  559.     BEGIN
  560.     IF fNextHandler <> NIL THEN
  561.         fNextHandler.PostCommand(command)
  562.     ELSE
  563.         BEGIN
  564.         {$IFC qDebug}
  565.         WrLblHexLongint('No one posted the command ', ord(command));
  566.         {$ENDC}
  567.         IF command.fFreeOnCompletion THEN
  568.             FreeIfObject(command);
  569.         END;
  570.     END;
  571.  
  572. {--------------------------------------------------------------------------------------------------}
  573. {$S MATerminate}
  574.  
  575. PROCEDURE TEvtHandler.Terminate;
  576.  
  577.     BEGIN
  578.     END;
  579.  
  580. {--------------------------------------------------------------------------------------------------}
  581. {$S MAEvtHandlerRes}
  582.  
  583. PROCEDURE TEvtHandler.KeyEventToComponents(VAR info: EventInfo);
  584.     BEGIN
  585.     IF fNextHandler <> NIL THEN
  586.         fNextHandler.KeyEventToComponents(info)
  587.     ELSE
  588.         BEGIN
  589.         WITH info, thePEvent^ DO
  590.             IF (what = keyDown) | (what = autoKey) THEN
  591.                 BEGIN
  592.                 { Default extractions }
  593.                 theCharacter := chr(BAND(message, charCodeMask));
  594.                 theKeycode := BSR(BAND(message, keyCodeMask), 8);
  595.                 END;
  596.         END;
  597.     END;
  598.  
  599.